package cn.hsa.xxx.bookData.bo.impl;


import cn.hsa.xxx.bookData.bo.BookTypeBO;
import cn.hsa.xxx.bookData.dao.BookTypeDAO;
import cn.hsa.xxx.api.dto.BookTypeDTO;
import cn.hsa.xxx.bookData.dto.BookTypeDTOConverter;
import cn.hsa.xxx.bookData.entity.BookTypeDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class BookTypeBOImpl implements BookTypeBO {

    @Resource
    BookTypeDAO bookTypeDAO;

    @Autowired
    BookTypeDTOConverter bookTypeDTOConverter;

    @Override
    public BookTypeDTO add(BookTypeDTO bookTypeDTO) {
        BookTypeDO bookTypeDO = bookTypeDTOConverter.convertToBookTypeDO(bookTypeDTO);
        bookTypeDAO.insert(bookTypeDO);
        return bookTypeDTOConverter.convertToBookTypeDTO(bookTypeDO);
    }

    @Override
    public String delete(String id) {
        bookTypeDAO.deleteById(id);
        return "删除成功";
    }

    @Override
    public BookTypeDTO get(String id) {
        BookTypeDO bookTypeDO = bookTypeDAO.selectById(id);
        BookTypeDTO bookTypeDTO = bookTypeDTOConverter.convertToBookTypeDTO(bookTypeDO);
        return bookTypeDTO;
    }

    @Override
    public String update(BookTypeDTO bookTypeDTO) {
        BookTypeDO bookTypeDO = bookTypeDTOConverter.convertToBookTypeDO(bookTypeDTO);
        bookTypeDAO.updateById(bookTypeDO);
        return "更新成功";
    }

    @Override
    public List<BookTypeDTO> getPage(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<BookTypeDO> bookTypeDOList = bookTypeDAO.selectList(null);

        PageInfo pageInfo = new PageInfo(bookTypeDOList);
        long totalRecords = pageInfo.getTotal();
        long totalPages = pageInfo.getPages();

        List<BookTypeDTO> bookTypeDTOList = new ArrayList<>();
        for (BookTypeDO bookTypeDO : bookTypeDOList) {
            bookTypeDTOList.add(bookTypeDTOConverter.convertToBookTypeDTO(bookTypeDO));
        }

        return bookTypeDTOList;
    }
}
